package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.TbSpecParam;
import com.leyou.item.mapper.TbSpecParamMapper;
import com.leyou.item.service.TbSpecParamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 规格参数组下的参数名 服务实现类
 * </p>
 *
 * @author HM
 * @since 2019-10-25
 */
@Service
public class TbSpecParamServiceImpl extends ServiceImpl<TbSpecParamMapper, TbSpecParam> implements TbSpecParamService {



    /*查询规格参数信息*/
    @Override
    public List<SpecParamDTO> findSpecParam(Long gid,Long cid, Boolean searching) {

        /*构造查询条件*/
        QueryWrapper<TbSpecParam> queryWrapper=new QueryWrapper<>();

        /*判断处理查询条件*/
        if(gid!=null && gid!=0){
            queryWrapper.lambda().eq(TbSpecParam::getGroupId,gid);
        }
        if(cid!=null && cid!=0){
            queryWrapper.lambda().eq(TbSpecParam::getCid,cid);
        }
        if(searching!=null){
            queryWrapper.lambda().eq(TbSpecParam::getSearching,searching);
        }
        List<TbSpecParam> tbSpecParamList = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(tbSpecParamList)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        List<SpecParamDTO> specParamDTOList = BeanHelper.copyWithCollection(tbSpecParamList, SpecParamDTO.class);

        return specParamDTOList;
    }

    @Override
    public void insertParam(SpecParamDTO specParamDTO) {
        /*对象的转化*/
        TbSpecParam tbSpecParam = BeanHelper.copyProperties(specParamDTO, TbSpecParam.class);

        /*保存数据*/
        boolean save = this.save(tbSpecParam);
        if(!save){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public void updateParam(SpecParamDTO specParamDTO) {
        TbSpecParam tbSpecParam = BeanHelper.copyProperties(specParamDTO, TbSpecParam.class);
        boolean update= this.updateById(tbSpecParam);
        if(!update){
            throw  new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public void deleteParam(Long id) {

        boolean remove = this.removeById(id);
        if(!remove){
            throw  new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }
}
